﻿using System;
using System.Collections.Generic;
using System.Linq;
using Colligate;
using System.Data.Objects;

namespace SysDataSource
{
    public class ConfigDS:ISysDS
    {
        #region\\delete
        public int DeleteTaskData(OUDataInfo userInfo, string dataGuid, string dataType, WebFramework context)
        {
            //switch (dataType.ToLower())
            //{
            //    case "company": return DeleteCompany(dataGuid, context);
            //    default: throw new Exception("CompanyDS无法处理该" + dataType + "的数据");
            //}
            return 1;
        }

        private static int DeleteCompany(string dataGuid, WebFramework context)
        {
            //try
            //{
            //    var company = context.Company.FirstOrDefault(cc => cc.Guid == dataGuid);
            //    context.DeleteObject(company);
            //    var result = context.SaveChanges();
            //    return result == 1 ? 0 : 1;
            //}
            //catch
            //{
            //    throw new Exception("删除数据标识为：" + dataGuid + "的Company数据时出错！");
            //}
            return 1;
        }
        #endregion
        #region\\get
        public System.Xml.XmlNode GetTaskData(OUDataInfo userInfo, string dataGuid, string dataType, WebFramework context, BaseTaskDataSource baseInfo)
        {
            switch (dataType.ToLower())
            {
                case "config": return GetCompany(dataGuid, context);
                default: EnvironmentInfo.WriteLogInfo("CompanyDS无法处理该" + dataType + "的数据", DebugLevel.CodePoint); return null;
            }
        }

        private static System.Xml.XmlNode GetCompany(string dataGuid, WebFramework context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "Config");
                var config = context.T_WF_Sys_Config.FirstOrDefault(cc => cc.Guid == dataGuid) ?? new T_WF_Sys_Config
                {
                    Guid = string.Empty,
                    Name = string.Empty
                };
                XmlNodeManage.SetSubValue(result, "Guid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), config.Guid);
                XmlNodeManage.SetSubValue(result, "Name", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), config.Name);
                return result;
            }
            catch
            {
                throw new Exception("获取数据标识为：" + dataGuid + "的Company数据时出错！");
            }
        }
        private static System.Xml.XmlNode GetCompanyList(string dataGuid, WebFramework context)
        {
            return null;
        }
        private static System.Xml.XmlNode GetCompanyAppList(string dataGuid, WebFramework context)
        {
            return null;
        }
        private static System.Xml.XmlNode GetCompanyBranchs(string dataGuid, WebFramework context)
        {
            return null;
        }

        #endregion
        #region\\set
        public int SetTaskData(OUDataInfo userInfo, string dataGuid, string dataType, System.Xml.XmlNode xmlData, WebFramework context)
        {
            return 1;
        }

        private static int SetCompany(System.Xml.XmlNode xmlData, WebFramework context) 
        {
            return 1;
        }
        #endregion
        #region\\transform
        public System.Xml.XmlNode TransformTaskData(OUDataInfo userInfo, System.Xml.XmlNode xmlParam, WebFramework context, BaseTaskDataSource baseInfo)
        {
            return null;
        }
        #endregion
    }
}
